#include<bits/stdc++.h>
using namespace std;
const int N = 1e6+10;
vector<int> bian[N];
int son[N];
int mdep[N];
int fath[N];
void dfs1(int x,int fa){
    fath[x] = fa;
    mdep[x] = 1;
    for(int v : bian[x]){
        if(v == fa){
            continue;
        }
        dfs1(v,x);
        mdep[x] = max(mdep[x],mdep[v] + 1);
        if(mdep[v] > mdep[son[x]]){
            son[x] = v;
        }
    }
}
int dfn[N],dft,rnk[N];
int d[N];
pair<int,int> bes[N];
pair<int,int> chkbest(pair<int,int> p1,pair<int,int> p2){
    if(p1.first != p2.first){
        return p1.first > p2.first?p1:p2;
    }else{
        return p1.second < p2.second?p1:p2;
    }
}
void dfs2(int x,int tf){
    dfn[x] = ++dft;
    rnk[dfn[x]] = x;
    if(son[x]){
        dfs2(son[x],tf);
        bes[x] = bes[son[x]];
        bes[x].second++;
    }
    d[dfn[x]] = 1;
    bes[x] = chkbest(bes[x],make_pair(d[dfn[x]],0));
    for(int v : bian[x]){
        if(v == fath[x] || v == son[x]){
            continue;
        }
        dfs2(v,v);
        for(int i = 1;i <= mdep[v];i++){
            d[dfn[x] + i] += d[dfn[v] + i - 1];
            bes[x] = chkbest(bes[x],make_pair(d[dfn[x] + i],i));
        }
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin>>n;
    for(int i = 1;i<n;i++){
        int u,v;
        cin>>u>>v;
        bian[u].push_back(v);
        bian[v].push_back(u);
    }
    dfs1(1,0);
    dfs2(1,1);
    for(int i = 1;i<=n;i++){
        cout<<bes[i].second<<'\n';
    }
}